/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.error;

import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import javax.swing.JTextArea;
import org.jfonia.constants.ViewConstants;

/**
 *
 * @author Rik Bauwens
 */
public class Console extends FilterOutputStream
{
        private JTextArea console;

        protected Console()
        {
                super(new ByteArrayOutputStream());
                System.setOut(new PrintStream(this));
                System.setErr(new PrintStream(this));
                console = new JTextArea();
                console.setRows(ViewConstants.ERROR_CONSOLE_ROWS);
                console.setColumns(ViewConstants.ERROR_CONSOLE_COLUMNS);
                console.setEditable(false);
        }

        public JTextArea getConsole()
        {
                return console;
        }

        @Override
        public String toString()
        {
                return console.getText();
        }

        /* Override Ancestor method */
        @Override
        public void write(byte b[]) throws IOException
        {
                String str = new String(b);
                logMessage(str);
        }

        /* Override Ancestor method */
        @Override
        public void write(byte b[], int off, int len) throws IOException
        {
                String str = new String(b, off, len);
                logMessage(str);
        }

        /* Override Ancestor method */
        @Override
        public void write(int b) throws IOException
        {
                String str = new String(new char[] { (char) b});
                logMessage(str);
        }

        private void logMessage(String message)
        {
                console.append(message);
        }

}
